﻿using AiShow.IDAL;
using AiShow.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiShow
{
    public class UserService: IUserService
    {
        public TBUsers FindByUserId(int userId)
        {
            AiShowDbContext dbContext = new AiShowDbContext();
            return dbContext.TBUsers.SingleOrDefault(m => m.Id == userId);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public TBUsers EditWithoutAvatar(TBUsers profile)
        {
            // 查询数据库
            AiShowDbContext dbContext = new AiShowDbContext();
            TBUsers userEntity = dbContext.TBUsers.SingleOrDefault(m => m.Id == profile.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.NickName = profile.NickName;
            userEntity.Sex = profile.Sex;
            userEntity.Birthday = profile.Birthday;
            userEntity.Address = profile.Address;
            userEntity.Hobby = profile.Hobby;
            // 更新到数据库
            dbContext.Entry(userEntity).State = EntityState.Modified;
            dbContext.SaveChanges();
            // 返回结果
            return userEntity;
        }

        /// <summary>
        /// 编辑头像
        /// </summary>
        /// <param name="profile"></param>
        /// <returns></returns>
        public TBUsers EditAvatar(TBUsers profile)
        {
            // 查询数据库
            AiShowDbContext dbContext = new AiShowDbContext();
            TBUsers userEntity = dbContext.TBUsers.SingleOrDefault(m => m.Id == profile.Id);
            if (userEntity == null)
            {
                return null;
            }
            userEntity.Avatar = profile.Avatar;
            // 更新到数据库
            dbContext.Entry(userEntity).State = EntityState.Modified;
            dbContext.SaveChanges();
            // 返回结果
            return userEntity;
        }
    }
}
